
package dal;

import entity.user;
import java.io.File;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class loadUserDoc {

    private Vector<user> users = new Vector<user>();

    public Vector<user> getUsers() {
        return users;
    }

    private String html = "";

    public String html(){
        return html;
    }

    public loadUserDoc(){
        try {
            String file = "http://localhost:8080/DemoDOM/XMLData/taikhoan.xml";

            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(new File("taikhoan.xml"));
            getData(doc);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    private user u;

    private void getData(Node node){
        if(node == null){
            return;
        }
        if(node.getNodeName().equals("name")){
            u = new user();
            u.setUsername(node.getTextContent());
        }
        if(node.getNodeName().equals("pass")){
            u.setPassword(node.getTextContent());
        }
        if(node.getNodeName().equals("full")){
            u.setFullname(node.getTextContent());
        }
        if(node.getNodeName().equals("role")){
            u.setRole(node.getTextContent());
            users.add(u);
        }
        NodeList childen = node.getChildNodes();
        for (int i= 0; i < childen.getLength(); i++) {
            getData(childen.item(i));
        }
    }
}
